package org.jeecg.modules.oss.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.mapper.OssFileMapper;
import org.jeecg.modules.oss.service.IOssFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: OSS云存储实现类
 * @author: jeecg-boot
 */
@Service("ossFileService")
public class OssFileServiceImpl extends ServiceImpl<OssFileMapper, OssFile> implements IOssFileService {

	@Override
	public Result<?> upload(MultipartFile multipartFile) throws Exception {
		String fileName = multipartFile.getOriginalFilename();
		fileName = CommonUtils.getFileName(fileName);
		OssFile ossFile = new OssFile();
		ossFile.setFileName(fileName);
		String url = OssBootUtil.upload(multipartFile,"upload/prod/" + getFormatNow());
		//update-begin--Author:scott  Date:20201227 for：JT-361【文件预览】阿里云原生域名可以文件预览，自己映射域名kkfileview提示文件下载失败-------------------
		// 返回阿里云原生域名前缀URL
		ossFile.setUrl(OssBootUtil.getOriginalUrl(url));
		//update-end--Author:scott  Date:20201227 for：JT-361【文件预览】阿里云原生域名可以文件预览，自己映射域名kkfileview提示文件下载失败-------------------
		String ossFileId = IdWorker.getIdStr();
		ossFile.setId(ossFileId);
		this.save(ossFile);
		Map<String, String> map = new HashMap<>();
		map.put("url", url);
		map.put("fileName", fileName);
		map.put("ossFileId", ossFileId);
		return Result.OK("上传成功", map);
	}

	@Override
	public Result<?> thread(MultipartFile multipartFile) throws Exception{
		String fileName = multipartFile.getOriginalFilename();
		fileName = CommonUtils.getFileName(fileName);
		String url = OssBootUtil.thread(multipartFile);
		//写入表
		OssFile ossFile = new OssFile();
		ossFile.setFileName(fileName);
		ossFile.setUrl(url);
		String ossFileId = IdWorker.getIdStr();
		ossFile.setId(ossFileId);
		//视频快照：使用fast模式截取视频1s处的内容，输出为JPG格式的图片，宽度为760，高度为400
		String snapshot = url + "?x-oss-process=video/snapshot,t_1,f_jpg,w_760,h_400,m_fast";
		ossFile.setSnapshot(snapshot);
		this.save(ossFile);

		Map<String, String> map = new HashMap<>();
		map.put("url", url);
		map.put("snapshot", snapshot);
		map.put("fileName", fileName);
		map.put("ossFileId", ossFileId);
		// 返回上传文件的URL地址
		return Result.OK("视频文件上传成功", map);
	}


	@Override
	public boolean delete(OssFile ossFile) {
		try {
			this.removeById(ossFile.getId());
			OssBootUtil.deleteUrl(ossFile.getUrl());
		}
		catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return false;
		}
		return true;
	}

	@Override
	public Map<String, Object> uploadFile(MultipartFile multipartFile) throws Exception {
		String fileName = multipartFile.getOriginalFilename();
		fileName = CommonUtils.getFileName(fileName);
		OssFile ossFile = new OssFile();
		ossFile.setFileName(fileName);
		String url = OssBootUtil.upload(multipartFile,"upload/prod/" + getFormatNow());
		//update-begin--Author:scott  Date:20201227 for：JT-361【文件预览】阿里云原生域名可以文件预览，自己映射域名kkfileview提示文件下载失败-------------------
		// 返回阿里云原生域名前缀URL
		ossFile.setUrl(OssBootUtil.getOriginalUrl(url));
		//update-end--Author:scott  Date:20201227 for：JT-361【文件预览】阿里云原生域名可以文件预览，自己映射域名kkfileview提示文件下载失败-------------------
		this.save(ossFile);
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "操作成功！");
		map.put("fileName", url);
		map.put("code", 0);
		return map;
	}

	public String getFormatNow(){
		// 获取当前日期
		LocalDate currentDate = LocalDate.now();
		// 定义日期格式（例如：2023-03-15）
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		// 格式化当前日期为字符串
		String formattedDate = currentDate.format(formatter);
		return formattedDate;
	}

	public static void main(String[] args) {
		YearMonth yearMonth = YearMonth.now();
		int year = yearMonth.getYear();
		int month = yearMonth.getMonthValue();
		System.out.println(year + String.format("%02d", month));
		// 获取当前日期
		LocalDate currentDate = LocalDate.now();
		// 定义日期格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		// 格式化当前日期为字符串
		String formattedDate = currentDate.format(formatter);
		System.out.println(formattedDate);
	}
}
